package com.itheima.health.controller;

import ch.qos.logback.core.util.TimeUtil;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.ValidateCodeUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
@Api(tags = "手机端验证码接口")
public class MobileValidateCodeController {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /* 手机端发送验证码 */
    @PostMapping("/send")
    public Result sendCode(@RequestParam String type, @RequestParam String telephone) {
//        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        String code = "1234";
        if(type.equals("MOBILE_SIGNIN")){
            log.info("用户电话:" + telephone + "的登录验证码:" + code);
        }else {
            log.info("用户电话:" + telephone + "的预约验证码:" + code);
        }
        redisTemplate.opsForValue().set(telephone, code, 60, TimeUnit.SECONDS);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
}
